package com.atguigu.yygh.order.controller;

import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.hosp.globalexp.YyghException;
import com.atguigu.yygh.hosp.result.R;
import com.atguigu.yygh.hosp.util.JwtHelper;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.mapper.OrderInfoMapper;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.order.service.WeixinService;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderCountVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/order")
public class OrderInfoController {
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    WeixinService weixinService;

    @Autowired
    OrderInfoMapper orderInfoMapper;
    /**
     * 查询第二天的订单数
     */
    @PostMapping("/getOrderCount")
    public Map getOrderCount(@RequestBody OrderCountQueryVo orderCountQueryVo){
        //map=dateList+countList
        Map map=orderInfoService.getOrderCount(orderCountQueryVo);
        return map;
    }



    /**
     * 点击取消按钮时调用
     */
    @GetMapping("/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable Long orderId){
        orderInfoService.cancelOrder(orderId);
        return R.ok();
    }

    /**
     * 前端显示支付二维码之后,开始定时调用该接口
     * 查询订单的支付状态,根据订单的id
     */
    @GetMapping("/queryPayStatus/{orderId}")
    public R queryPayStatus(@PathVariable Long orderId){
        //调用微信端的"查询订单"接口返回了map
       Map<String,String> map = weixinService.queryPayStatus(orderId);
       if(map==null){
           throw new YyghException(20001,"微信端接口调用失败");
       }
        String trade_state = map.get("trade_state");
       if("SUCCESS".equalsIgnoreCase(trade_state)){

           //支付成功后需要做的工作
           orderInfoService.paySuccess(orderId,map);
           return R.ok().message("支付成功");
       }
        return R.ok().message("支付中");
    }

    /**
     * 根据用户id进行条件分页查询
     */
    @GetMapping("/selectPage/{pageNum}/{pageSize}")
    public R selectPage(@PathVariable Long pageNum,@PathVariable Long pageSize, OrderQueryVo orderQueryVo, HttpServletRequest request){
        //从请求头中解析用户id
        String token = request.getHeader("token");
        Long userId = JwtHelper.getUserId(token);
        if(userId!=null){
            orderQueryVo.setUserId(userId);
            //mp中的page
            Page<OrderInfo> page=orderInfoService.selectPage(pageNum,pageSize,orderQueryVo);
            //page.getRecords();
            //page.getTotal();
            return R.ok().data("pageModel",page);
        }
        return R.ok().data("pageModel",null);

    }

    /**
     * 查询订单状态列表
     * 获取枚举里面所有的值就是用values
     */
    @GetMapping("/getStatusList")
    public R getStatusList(){
        OrderStatusEnum[] values = OrderStatusEnum.values();   //values是获取枚举里面的所有值

        List<Map> list=new ArrayList<>();
        for (OrderStatusEnum e : values) {
            Integer status = e.getStatus();
            String comment = e.getComment();

            Map map=new HashMap<>();
            map.put("status",status);
            map.put("comment",comment);

            list.add(map);
        }
        return R.ok().data("statusList",list);
    }

    /**
     * 根据订单id查询订单
     * @param orderId
     * @return
     */
    @GetMapping("/getOrder/{orderId}")
    public R getOrder(@PathVariable("orderId") Long orderId){
        OrderInfo orderInfo = orderInfoService.getOrder(orderId);
        return R.ok().data("orderInfo",orderInfo);
    }

    @GetMapping("/checkExistOrder/{patientId}/{scheduleId}")
    public  R checkExistOrder(@PathVariable Long patientId,@PathVariable String scheduleId){

        //校验 就诊人+排班 在今天的取消订单的次数
        Integer count = orderInfoService.checkCancelOrderCount(patientId,scheduleId);
        if(count>=3){
            //不允许挂号
           // throw new YyghException(20001,"今日取消订单次数已到");
            return R.ok().message("该就诊人在该排班下今日的取消订单次数已达上限");
        }
        Long orderId =orderInfoService.checkExistOrder(patientId,scheduleId);
        if(orderId==null){
            //订单不存在,可以提交订单
            return R.ok().message("订单不存在");
        }else {
            return R.ok().message("订单已创建").data("orderId",orderId);
        }
    }

    /**
     * 提交订单
     * @param patientId  就诊人id
     * @param scheduleId Mongodb中排班的id
     * @return
     */
//    @PostMapping("/submitOrder/{patientId}/{scheduleId}")
//    public R submitOrder(@PathVariable Long patientId,@PathVariable String scheduleId){
//        //返回值是平台端创建的主键的id
//        Long orderId = orderInfoService.submitOrder(patientId,scheduleId);
//        return R.ok().data("orderId",orderId);
//    }


    /**
     * 传递的医院编号的用处 1.热点限流 2.可以检验前端传递的医院编号 和 后端查询的医院编号检查是否一致
     * @param patientId
     * @param scheduleId
     * @param hoscode
     * @return
     */
    @PostMapping("/submitOrder/{patientId}/{scheduleId}/{hoscode}")
    public R submitOrder(@PathVariable Long patientId,@PathVariable String scheduleId,@PathVariable String hoscode){
        //返回值是平台端创建的主键的id
        Long orderId = orderInfoService.submitOrder(patientId,scheduleId,hoscode);
        return R.ok().data("orderId",orderId);
    }
}
